<!--
 * @Author: qiye
 * @LastEditors: qiye
 * @description: page description
 * @Date: 2023-08-03 12:22:23
 * @LastEditTime: 2023-08-10 19:49:40
-->
<script setup lang="ts" name="PrintConfig">
import axios from 'axios'

interface IProps {
  id: string
  srcPrefix?: string
  token?: string
}

const props = withDefaults(defineProps<IProps>(), {
  srcPrefix: ''
})
const emit = defineEmits(['created'])
const isUpdate = computed<boolean>(() => !!props.id)

const state = reactive({
  currentId: props.id,
  loading: true,
  src: ''
})

/**
 * 创建
 */
const create = async () => {
  const { data }: any = await axios.post(`${props.srcPrefix ?? ''}/jmreport/save`, {}, {
    headers: {
      Authorization: props.token
    }
  })
  if (data.code !== 200)
    return
  const id = data.result.id
  state.currentId = id
  state.src = `${props.srcPrefix}/jmreport/index/${state.currentId}?token=${props.token}&menuType=printinfo`
  emit('created', state.currentId)
}

// 初始化系统
const init = () => {
  if (!isUpdate.value) {
    create()
    return
  }
  state.src = `${props.srcPrefix}/jmreport/index/${state.currentId}?token=${props.token}`
}

onMounted(() => {
  init()
})
</script>

<template>
  <iframe
    v-if="state.src"
    width="100%"
    height="100%"
    :src="state.src"
    frameborder="0"
  >
  </iframe>
</template>
